package com.hp.ongoing.persistence.factory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.hp.ongoing.entity.AutenticacaoUsuario;
import com.hp.ongoing.entity.UserCredentialVO;

public class ConnectionFactory {

	private static final String driverClassName = "oracle.jdbc.OracleDriver";
	private static String connectionUrl = null;//"jdbc:oracle:thin:@5.16.8.187:1521:db09";	
	private Connection connection = null;
	private String dbPwd;
	private String dbUser;
	
	public synchronized  Connection getConnection(AutenticacaoUsuario user) throws SQLException {
		if (connection == null){			
			connectionUrl = ConnectionStrategy.getConnectionProdMap().get(user.getBase());
			dbUser = user.getUsuario();
			dbPwd = user.getSenha();
			connection = DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
			currentDB = user.getBase();
			connection.setAutoCommit(true);
			System.out.println("CONECTADO NA BASE "+currentDB+" COM O USUARIO "+dbUser);
		}				
		return connection;
	}

	private static String currentDB;

	
	
	private static ConnectionFactory connectionFactory = null;

	private ConnectionFactory() {
		try {
			Class.forName(driverClassName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public Connection getConnection(String aliasBase) throws SQLException {
		
		Connection conn = null;
		return conn;
	}

	public static ConnectionFactory getInstance() {
		if (connectionFactory == null) {
			connectionFactory = new ConnectionFactory();
		}
		return connectionFactory;
	}

	public void encerraConexao() throws SQLException {
		if (connection != null){
			connection.close();
			connection = null;			
			System.out.println("Conexao encerrada");
		}
	}

}
